package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Pyq;
import com.example.service.PyqService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "朋友圈管理")
@CrossOrigin
@RequestMapping("/pyq")
@RestController
public class PyqController {

    @Autowired
    private PyqService pyqService;

    @ApiOperation(value = "分页查询朋友圈")
    @GetMapping("getPyq/{current}/{limit}")
    public R getPyq(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit
    ) {

        QueryWrapper<Pyq> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        //实现分页
        Page<Pyq> PyqPage = new Page<>(current, limit);
        IPage<Pyq> page = pyqService.page(PyqPage, wrapper);
        //获取分页后数据
        long total = page.getTotal();
        List<Pyq> list = page.getRecords();

        return R.ok().data("total", total).data("list", list);
    }

    @ApiOperation(value = "分页带条件查询朋友圈")
    @PostMapping("searchPyq/{current}/{limit}")
    public R searchPyq(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit,
            @RequestBody(required = false) Pyq pyq
    ) {

        QueryWrapper<Pyq> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(pyq)) {
            if (!StringUtils.isEmpty(pyq.getUserId())) {
                wrapper.eq("user_id", pyq.getUserId());
            }
        }
        wrapper.orderByDesc("gmt_create");
        //实现分页
        Page<Pyq> PyqPage = new Page<>(current, limit);
        IPage<Pyq> page = pyqService.page(PyqPage, wrapper);
        //获取分页后数据
        long total = page.getTotal();
        List<Pyq> list = page.getRecords();

        return R.ok().data("total", total).data("list", list);
    }

    @ApiOperation(value = "新增朋友圈")
    @PostMapping("addPyq/{userid}")
    public R addPyq(
            @ApiParam(name = "userid", value = "用户id", required = true) @PathVariable String userid,
            @ApiParam(name = "pyq", value = "朋友圈内容", required = true) @RequestBody Pyq pyq) {

        boolean b = pyqService.addPyq(userid, pyq);

        return R.ok();
    }

    @ApiOperation(value = "删除朋友圈")
    @DeleteMapping("deletePyq/{pyqId}")
    public R deletePyq(@PathVariable String pyqId) {

        pyqService.deletePyq(pyqId);

        return R.ok();
    }

}
